package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LittleGame extends JFrame {
    private static final int PANEL_WIDTH = 5;
    private static final int PANEL_HEIGHT = 5;
    private static final int PANEL_NUMBER = 2;
    private static final int WINDOW_WIDTH = 640;
    private static final int WINDOW_HEIGHT = 640;
    private static final int INDEX = -1;

    private JPanel[] p = new JPanel[PANEL_NUMBER];
    private JButton exitBtn;
    private JButton[][] colorBtn;
    private int step = 0;
    private int redBlock = 0;


    public LittleGame() {
        initComponent();
        addComponent();
        addListener();
        initWindows();
    }

    private void initComponent() {
        colorBtn = new JButton[PANEL_WIDTH][PANEL_HEIGHT];
        for (int i = 0; i < colorBtn.length; i++) {
            for (int j = 0; j < colorBtn[i].length; j++) {
                colorBtn[i][j] = new JButton();
                colorBtn[i][j].setActionCommand(i + "," + j);
                colorBtn[i][j].setBackground(Color.YELLOW);
            }
        }
        p[0] = new JPanel();
        p[1] = new JPanel();
        exitBtn = new JButton("结束");
    }

    private void addComponent() {
        p[0].setLayout(new GridLayout(5, 5));
        for (int i = 0; i < colorBtn.length; i++) {
            for (int j = 0; j < colorBtn[i].length; j++) {
                p[0].add(colorBtn[i][j]);
            }
        }
        exitBtn.setActionCommand("exit");
        p[1].setLayout(new BorderLayout());
        p[1].add(exitBtn, BorderLayout.CENTER);
        this.add(p[0]);
        this.add(p[1], BorderLayout.SOUTH);

    }

    private void addListener() {
        ChangeColorListener listener = new ChangeColorListener();
        for (int i = 0; i < colorBtn.length; i++) {
            for (int j = 0; j < colorBtn[i].length; j++) {
                colorBtn[i][j].addActionListener(listener);
            }
        }
        exitBtn.addActionListener(listener);
    }

    private void initWindows() {
        this.setTitle("小游戏");
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new LittleGame();
    }

    private class ChangeColorListener implements ActionListener {

        /**
         * Invoked when an action occurs.
         *
         * @param e the event to be processed
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();
            String position = e.getActionCommand();
            if ("exit".equals(position)) {
                System.exit(0);
            } else {
                int x = Integer.parseInt(position.split(",")[0]);
                int y = Integer.parseInt(position.split(",")[1]);
                System.out.println(x + "," + y);
                step++;
                if (x - 1 > INDEX) {
                    if (Color.YELLOW.equals(colorBtn[x - 1][y].getBackground())) {
                        colorBtn[x - 1][y].setBackground(Color.RED);
                    } else {
                        colorBtn[x - 1][y].setBackground(Color.YELLOW);
                    }
                }
                if (x + 1 < PANEL_HEIGHT) {
                    if (Color.YELLOW.equals(colorBtn[x + 1][y].getBackground())) {
                        colorBtn[x + 1][y].setBackground(Color.RED);
                    } else {
                        colorBtn[x + 1][y].setBackground(Color.YELLOW);
                    }
                }
                if (y - 1 > INDEX) {
                    if (Color.YELLOW.equals(colorBtn[x][y - 1].getBackground())) {
                        colorBtn[x][y - 1].setBackground(Color.RED);
                    } else {
                        colorBtn[x][y - 1].setBackground(Color.YELLOW);
                    }
                }
                if (y + 1 < PANEL_WIDTH) {
                    if (Color.YELLOW.equals(colorBtn[x][y + 1].getBackground())) {
                        colorBtn[x][y + 1].setBackground(Color.RED);
                    } else {
                        colorBtn[x][y + 1].setBackground(Color.YELLOW);
                    }
                }
                if (Color.YELLOW.equals(colorBtn[x][y].getBackground())) {
                    colorBtn[x][y].setBackground(Color.RED);
                } else {
                    colorBtn[x][y].setBackground(Color.YELLOW);
                }

            }
            for (int i = 0; i < colorBtn.length; i++) {
                for (int j = 0; j < colorBtn[i].length; j++) {
                    if (Color.RED.equals(colorBtn[i][j].getBackground())) {
                        redBlock++;
                    }
                }
            }
            if (redBlock == PANEL_HEIGHT * PANEL_WIDTH) {
                int checkWin = JOptionPane.showConfirmDialog(null, "共操作" + step + "步，是否继续？", "胜利", JOptionPane.YES_NO_OPTION);
                if (checkWin == JOptionPane.NO_OPTION) {
                    System.exit(0);
                } else {
                    dispose();
                    new LittleGame();
                }
            }
            redBlock = 0;
        }
    }
}
